package com.siblings.loan.utils.dialog

import android.Manifest
import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.siblings.lib_permission.OnPermissionCallback
import com.siblings.lib_permission.XXPermissions
import com.siblings.loan.R
import com.siblings.loan.databinding.DialogPhotoBinding
import com.siblings.loan.http.HttpHelper
import com.siblings.loan.utils.event.SibEventType

/**
 * Siblings
 * @Author wwdeng
 * @CreateTime: 2024/7/12 17:59
 * @Annotation：拍照、相册选择 dialog
 */
class PhotoDialog: ProDialog {
    private lateinit var binding: DialogPhotoBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""
    //选择类型
    private var selectIndex = 1

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context?, remark: String,callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    override fun initWindow() {
        val params = window!!.attributes
        params.width =  WindowManager.LayoutParams.MATCH_PARENT
        params.height = WindowManager.LayoutParams.WRAP_CONTENT
        window!!.attributes = params
        window!!.setWindowAnimations(R.style.dialog_slide_anim) //修改动画样式
        window!!.setGravity(Gravity.BOTTOM)
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = DialogPhotoBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
        selectIndex = 1
        checkSibStateViews()
    }

    override fun initListener() {
        setOnClickListener(R.id.btnConfirm)//确认
        //拍照
        binding.cameraLayout.setOnClickListener {
            selectIndex = 1
            checkSibStateViews()
        }
        //相册选择
        binding.albumLayout.setOnClickListener {
            selectIndex = 2
            checkSibStateViews()
        }
    }

    override fun initData() {

    }

    override fun onViewClick(v: View?) {
        dismiss()
        if (v!!.id == R.id.btnConfirm){
            when(selectIndex){
                1 -> {//拍照
                    showSibCameraClick()
                }
                 else -> {//相册
                    showSibAlbumClick()
                 }
            }
        }
    }

    /**
     * 切换选择状态
     */
    private fun checkSibStateViews(){
        binding.cameraLayout.setBackgroundResource(R.drawable.sib_radius_fill_white_12)
        binding.textTakePhoto.setTextColor(Color.parseColor("#FF999999"))
        binding.textTakePhoto.setTypeface(Typeface.DEFAULT,Typeface.NORMAL)
        binding.imgTakePhoto.visibility = View.INVISIBLE

        binding.albumLayout.setBackgroundResource(R.drawable.sib_radius_fill_white_12)
        binding.textAlbum.setTextColor(Color.parseColor("#FF999999"))
        binding.textAlbum.setTypeface(Typeface.DEFAULT,Typeface.NORMAL)
        binding.imgAlbum.visibility = View.INVISIBLE

        when(selectIndex){
            1 -> {
                binding.cameraLayout.setBackgroundResource(R.drawable.sib_photo_dialog_select)
                binding.textTakePhoto.setTextColor(Color.parseColor("#FF4DB673"))
                binding.textTakePhoto.setTypeface(Typeface.DEFAULT,Typeface.BOLD)
                binding.imgTakePhoto.visibility = View.VISIBLE
            }
            else -> {
                binding.albumLayout.setBackgroundResource(R.drawable.sib_photo_dialog_select)
                binding.textAlbum.setTextColor(Color.parseColor("#FF4DB673"))
                binding.textAlbum.setTypeface(Typeface.DEFAULT,Typeface.BOLD)
                binding.imgAlbum.visibility = View.VISIBLE
            }
        }
    }

    /**
     * 拍照
     */
    private fun showSibCameraClick(){
        val permissions = arrayListOf(
            Manifest.permission.CAMERA
        )
        if (XXPermissions.isGranted(context,permissions)){
            if(mCallback != null){
                mCallback!!.openCamera()
            }
        } else {
            XXPermissions.with(context)
                // 申请多个权限
                .permission(permissions)
                .request(object: OnPermissionCallback {
                    override fun onGranted(p0: MutableList<String>, p1: Boolean) {
                        var permissionString = ""
                        p0.forEach {
                            permissionString = "$permissionString$it,"
                        }
                        HttpHelper.addSibEventAction(SibEventType.CLICK.SIB_CLICK_PERMISSION_SYSTEM_AGREE,permissionString.substring(0,permissionString.length - 1))
                        if(mCallback != null){
                            mCallback!!.openCamera()
                        }
                    }
                    override fun onDenied(p0: MutableList<String>, doNotAskAgain: Boolean) {
                        var permissionString = ""
                        p0.forEach {
                            permissionString = "$permissionString$it,"
                        }
                        HttpHelper.addSibEventAction(SibEventType.CLICK.SIB_CLICK_PERMISSION_SYSTEM_DENY,permissionString.substring(0,permissionString.length - 1))
                        XXPermissions.startPermissionActivity(context,permissions)
                    }
                })
        }
    }

    /**
     * 相册
     */
    private fun showSibAlbumClick(){
        if(mCallback != null){
            mCallback!!.openAlbum()
        }
    }

    interface IDialogCallback {
        //打开相机
        fun openCamera()
        //打开相册
        fun openAlbum()
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addSibEventAction(SibEventType.DIALOG.SIB_DIALOG_PHOTO_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        HttpHelper.addSibEventAction(SibEventType.DIALOG.SIB_DIALOG_PHOTO_DISMISS,eventRemark)
    }
}